import 'dart:math';
import 'dart:ui';

import 'package:eh_mobile/common/utils/extension.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../controller.dart';

typedef DeviveTypeButtonCallback = void Function(int index);

class DeviveDynamicButton extends StatefulWidget {
  const DeviveDynamicButton(
      {super.key,
      required this.icon,
      required this.title,
      required this.content,
      required this.onPressed,
      this.height = 42,
      this.index = 0});
  final Widget icon;
  final String title;
  final String content;
  final double height;
  final int index;
  final DeviveTypeButtonCallback onPressed;

  @override
  State<DeviveDynamicButton> createState() => _DeviveDynamicButtonState();
}

class _DeviveDynamicButtonState extends State<DeviveDynamicButton>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return _DeviveDynamicButtonGetX(
      icon: widget.icon,
      title: widget.title,
      content: widget.content,
      height: widget.height,
      index: widget.index,
      onPressed: widget.onPressed,
    );
  }
}

// 设备类型按钮
class _DeviveDynamicButtonGetX extends GetView<HomepageController> {
  const _DeviveDynamicButtonGetX({
    Key? key,
    required this.icon,
    required this.title,
    required this.content,
    required this.height,
    required this.index,
    required this.onPressed,
  }) : super(key: key);

  final Widget? icon;
  final String? title;
  final String? content;
  final double? height;
  final int? index;
  final DeviveTypeButtonCallback onPressed;
  @override
  Widget build(BuildContext context) {
    double titleWidth = calculateTextHeight(
        context, title!, FontWeight.bold, 14.0, 1.sw - 24, 1);
    double contentWidth = calculateTextHeight(
        context, content!, FontWeight.w500, 10.0, 1.sw - 24, 1);

    return InkWell(
      onTap: () => onPressed(index!),
      child:
          //循环(
          //         sigmaX: 5.0,
          //         sigmaY: 5.0,
          //       ),
          //       child:

          Stack(
        children: [
          // GlassContainer.frostedGlass(
          //   height: height!.h,
          //   width: max(titleWidth, contentWidth) + 70.w,
          //   blur: 5,
          //   borderRadius: BorderRadius.circular(22),
          //   borderWidth: 1,
          //   borderColor: Color(0xffFFC44B),
          //   color: const Color.fromRGBO(155, 120, 50, 0.4),
          //   frostedOpacity: 0.05,
          //   elevation: 3.0,
          // ),
          // Container(
          //   clipBehavior: Clip.antiAlias,
          //   decoration: BoxDecoration(
          //       borderRadius: BorderRadius.circular(22).r,
          //       border: Border.all(width: 1, color: const Color(0xffFFC44B))),
          //   child: BackdropFilter(
          //     filter: ImageFilter.blur(
          //         sigmaX: 1, sigmaY: 1, tileMode: TileMode.clamp),
          //     child: Container(
          //       color: const Color.fromRGBO(155, 120, 50, 0.5),
          //       width: max(titleWidth, contentWidth) + 66.w,
          //       height: height!.r,
          //     ),
          //   ),
          // ),

          Container(
            height: height!.r,
            padding: const EdgeInsets.only(left: 16, right: 16).r,
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(22).r,
                border: Border.all(width: 1, color: const Color(0xffFFC44B))),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(
                  width: 22.r,
                  height: 22.r,
                  child: icon,
                ),
                10.sizedBoxWidth,
                Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SizedBox(
                      height: 18.h,
                      child: Text(title!,
                          style: 14.ts.copyWith(fontWeight: FontWeight.bold)),
                    ),
                    SizedBox(
                      // height: 13.h,
                      child: Text(content!,
                          style: 10.ts.copyWith(
                              color: const Color.fromRGBO(235, 235, 235, 0.8),
                              fontWeight: FontWeight.w500)),
                    ),
                  ],
                )
              ],
              //   ),
              // ),
            ),
          ),
        ],
      ),

      // GlassContainer.frostedGlass(
      //     height: height!.h,
      //     width: max(titleWidth, contentWidth) + 70.w,
      //     blur: 5,
      //     borderRadius: BorderRadius.circular(22),
      //     borderWidth: 1,
      //     borderColor: Color(0xffFFC44B),
      //     color: const Color.fromRGBO(155, 120, 50, 0.4),
      //     frostedOpacity: 0.05,
      //     elevation: 3.0,
      //     child: Container(
      //       padding: const EdgeInsets.only(left: 16, right: 16).r,
      //       // color: const Color.fromRGBO(155, 120, 50, 0.4),
      //       child: Row(
      //         crossAxisAlignment: CrossAxisAlignment.center,
      //         children: [
      //           SizedBox(
      //             width: 22.r,
      //             height: 22.r,
      //             child: icon,
      //           ),
      //           10.sizedBoxWidth,
      //           Column(
      //             mainAxisAlignment: MainAxisAlignment.center,
      //             crossAxisAlignment: CrossAxisAlignment.start,
      //             children: [
      //               SizedBox(
      //                 height: 18.h,
      //                 child: Text(title!,
      //                     style: 14
      //                         .ts
      //                         .copyWith(fontWeight: FontWeight.bold)),
      //               ),
      //               SizedBox(
      //                 // height: 13.h,
      //                 child: Text(content!,
      //                     style: 10.ts.copyWith(
      //                         color:
      //                             const Color.fromRGBO(200, 200, 200, 1),
      //                         fontWeight: FontWeight.w500)),
      //               ),
      //             ],
      //           )
      //         ],
      //         //   ),
      //         // ),
      //       ),
      //     ))
    );
  }

  ///value: 文本内容；fontSize : 文字的大小；fontWeight：文字权重；maxWidth：文本框的最大宽度；maxLines：文本支持最大多少行
  double calculateTextHeight(BuildContext context, String value,
      FontWeight fontWeight, double? fontSize, double maxWidth, int maxLines) {
    if (value.isEmpty) {
      return 0.0;
    }

    TextPainter painter = TextPainter(
      locale: Localizations.localeOf(context),
      maxLines: maxLines,
      textDirection: TextDirection.ltr,
      text: TextSpan(
        text: value,
        style: TextStyle(
          fontSize: fontSize,
          fontWeight: fontWeight,
        ),
      ),
    );

    painter.layout(maxWidth: maxWidth);
    return painter.width;
  }
}
